<?php
namespace app\admin\controller;
use catetree\Catetree;
class GoodsCategory extends Common
{
    public function lst()
    {
        $_cateRes=db('goods_category')->order('sort,id')->select();
        $CateTree=new Catetree();
        $cateRes=$CateTree->catetree($_cateRes);
        foreach ($cateRes as $k=>$v){
            $cateRes[$k]['is'] = db('goods_category')->where('pid',$v['id'])->find();
        }
        $this->assign('cateRes',$cateRes);
        $count=db('goods_category')->count();
        $this->assign('count',$count);
        return view();
    }

    public function add()
    {
        if(request()->isAjax()){
            $data=input('post.')['data'];
            unset($data['file']);
            $add=db('goods_category')->insert($data);
            if($add){
                return ['msg'=>'新增产品分类成功','icon'=>1];
            }else{
                return ['msg'=>'新增产品分类失败','icon'=>2];
            }
            return;
        }
        //用于上级产品分类的显示
        $_cateRes=db('goods_category')->order('sort')->select();
        $CateTree=new Catetree();
        $cateRes=$CateTree->catetree($_cateRes);
        //lst页面点击添加子分类的情况
        $cateid=input('cate_id');
        $this->assign([
            'cateRes'=>$cateRes,
            'cateid'=>$cateid
        ]);
        return view();
    }

    public function edit()
    {
        if(request()->isAjax()){
            $data=input('post.')['data'];
            unset($data['file']);
            $save=db('goods_category')->update($data);
            if($save !== false){
                return ['msg'=>'编辑产品分类成功','icon'=>1];
            }else{
                return ['msg'=>'编辑产品分类失败','icon'=>2];
            }
            return;
        }
        //用于上级产品分类的显示
        $_cateRes=db('goods_category')->order('sort')->select();
        $CateTree=new Catetree();
        $cateRes=$CateTree->catetree($_cateRes);
        //当前这条产品分类的信息
        $cates=db('goods_category')->find(input('id'));
        
        $this->assign([
            'cateRes'=>$cateRes,
            'cates'=>$cates
        ]);
        return view();
    }

    public function del()
    {
        if(request()->isAjax()){
            $id=input('id');
            $CateTree=new Catetree();
            $allId=$CateTree->childrenids('goods_category',$id);
            $allId[]=$id;
            $del=db('goods_category')->delete($allId);
            if($del){
                return ['msg'=>'删除产品分类成功','icon'=>1];
            }else{
                return ['msg'=>'删除产品分类失败','icon'=>2];
            }
        } 
    }
    //产品分类排序
    public function sort()
    {
        if(request()->isAjax()){
            $id=input('id');
            $sort=input('sort');
            $save=db('goods_category')->where(['id'=>$id])->update(['sort'=>$sort]);
            if($save !== false){
                return ['msg'=>'已排序','icon'=>1];
            }
        }

    }

        //layui图片上传
    public function upload(){
        $file = request()->file('file');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(GOODSCATEICONIMG);
        $data = array();  //定义一个返回的数组
        if($info){
            $reubfo['code']= 1;
            $reubfo['savename'] = $info->getSaveName(); 
        }else{
            // 上传失败获取错误信息
            $reubfo['code']= 0;
            $reubfo['err'] = $file->getError();
        } 
        return json_encode($reubfo);
    }

    //ajax栏目添加修改页面图片撤销功能 删除本地图片
    public function cancel(){
        $id=input('id');
        $imgurl=input('imgurl');
        $res=@unlink(GOODSCATEICONIMG.$imgurl);
        if($id){
            db('goods_category')->where('id',$id)->setField('icon','');
        }
        if($res){
            echo 1;
        }else{
            echo 2;
        }
    }



}
